11 const double infinity
= 1E20
;
15 point(double X
, double Y
){ x
= X
; y
= Y
;}
18 map
< point
, double > dist
;
20 bool operator ==(const point
&a
, const point
&b
){ return (a
.x
== b
.x
&& a
.y
== b
.y
);}
21 bool operator !=(const point
&a
, const point
&b
){ return !(a
== b
);}
22 bool operator <(const point
&a
, const point
&b
){ return (a
.x
< b
.x
|| (a
.x
== b
.x
&& a
.y
< b
.y
));}
23 double distancia(point a
, point b
){return hypot(a
.y
-b
.y
, a
.x
-b
.x
);}
26 struct heapCompare
: public binary_function
<point
, point
, bool>
28 bool operator()(const point
&x
, const point
&y
) const
29 { return dist
[x
] > dist
[y
]; }
34 //contiene todos los nodos sueltos
36 //contiene un vector con todos los vecinos para el punto point
37 map
< point
, vector
<point
> > vecinos
;
40 if (vecinos
.count(a
) == 1) return; //Ya insertamos este nodo
43 vecinos
.insert(make_pair(a
, v
));
46 void make_vecinos(double maxPath
){
47 for (map
< point
, vector
<point
> >::iterator it
=vecinos
.begin(); it
!=vecinos
.end(); ++it
){
48 if (distancia((*it
).first
, point(0.00, 0.00)) > maxPath
){
51 for (map
< point
, vector
<point
> >::iterator jt
= it
; jt
!=vecinos
.end(); ++jt
){
52 if ((*it
).first
!= (*jt
).first
){
53 if ((*jt
).first
.x
- (*it
).first
.x
> 1.5){
56 vector
<point
> adj
= vecinos
[(*it
).first
];
57 if (distancia((*jt
).first
, (*it
).first
) <= 1.5){
58 vecinos
[(*it
).first
].push_back((*jt
).first
);
59 vecinos
[(*jt
).first
].push_back((*it
).first
);
68 for (int i
=0; i
<nodos
.size(); ++i
){
69 dist
[nodos
[i
]] = infinity
;
70 if (nodos
[i
].x
== 0.00 && nodos
[i
].y
== 0.00){
71 dist
[nodos
[i
]] = 0.00;
76 void dijkstra(const double &maxPath
, const point
&finalPoint
){
79 priority_queue
<point
, vector
<point
>, heapCompare
> q
;
80 q
.push(point(0.0, 0.0));
84 if (distancia(point(0.00, 0.00), u
) + distancia(u
, finalPoint
) <= maxPath
){
85 for (int i
=0; i
<vecinos
[u
].size(); ++i
){
86 point v
= vecinos
[u
][i
];
87 if (dist
[vecinos
[u
][i
]] > (dist
[u
] + distancia(u
,v
))){
88 dist
[vecinos
[u
][i
]] = dist
[u
] + distancia(u
, v
);
104 for (s
= ""; s
== ""; getline(cin
, s
));
115 g
.insert(point((double)w
, (double)h
));
116 g
.insert(point(0.00, 0.00));
119 for (int i
=0; i
<noPuntos
; ++i
){
122 g
.insert(point(x
,y
));
129 g
.make_vecinos(maximoCamino
);
131 g
.dijkstra(maximoCamino
, point((double)w
, (double)h
));
132 if (dist
[point((double)w
, (double)h
)] <= maximoCamino
){
133 printf("I am lucky!\n");